package com.zmn.plat.admin.controller.contract.record;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.vo.contract.record.ContractRecordVO;

/**
 * 合同表
 *
 * @author linfeng
 *
 * @since 2020-05-09 11:41:45
 */
@RestController
@RequestMapping("/contract/record/")
public class ContractRecordModifyController extends ContractRecordBaseController {

    /**
     * 新增修改
     * 
     * @author linfeng
     * @param contractRecord
     * @since 2020-05-09 11:41:45
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/contract/record/addEdit.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ContractRecordVO contractRecord) {
        Integer contractId = contractRecord.getContractId();
        String opertor = getMcStaffRealName();

        try {
            if (contractId == null || contractId == 0) {
                contractRecordBService.insert(contractRecord, opertor);
            } else {
                contractRecordBService.update(contractRecord, opertor);
            }

            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功");
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception ex) {
            logger.error("新增/修改合同失败：contractRecord:{}", contractRecord, ex);
            return ResponseDTO.fail("操作失败");
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     * @author linfeng
     * @since 2020-05-14 10:23:38
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/standard/record/list.action")
    public ResponseDTO updateSort(HttpServletRequest request, @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }

        Integer result = contractRecordService.updateSortByKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }
}
